<template>
  <el-dialog title="用户编辑"
             :visible="visible"
             @close="close"
             :append-to-body="true"
             :close-on-click-modal="false">
    <el-table
      highlight-current-row
      ref="data_table"
      :data="data.data"
      :fit="true"
      :max-height="400"
      :border="true"
      @selection-change="selectionChange"
      style="width: 100%;margin: 10px 0px; ">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="name" label="角色名" width="200"></el-table-column>
      <el-table-column prop="description" label="描述"></el-table-column>
    </el-table>
    <el-pagination background
                   @current-change="loadDatas()"
                   @size-change="loadDatas()"
                   :total="data.total"
                   :current-page.sync="data.page"
                   :page-size.sync="data.size"
                   :hide-on-single-page="false"
                   :page-sizes="[1,10, 15, 20, 25,30, 35,40]"
                   layout="total, sizes, prev, pager, next, jumper"></el-pagination>
    <span slot="footer" class="dialog-footer">
    <el-button @click="close">取 消</el-button>
    <el-button type="primary" @click="onselected">确 定</el-button>
  </span>
  </el-dialog>
</template>

<script>
export default {
  name: "RoleSelector",
  props: {
    visible: {
      type: Boolean,
      default: true,
      required: false
    },
    filters: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      //用户保存用户输入的查询内容
      query: null,
      loaded: false,
      //用户列表数据
      data: {total: 0, data: [], size: 10, page: 1},
      selection: []
    };
  },
  mounted() {
    //初始化时加载用户数据
    //this.loadDatas();
  },
  methods: {
    onselected() {
      this.$emit("onselected", this.$refs.data_table.selection)
    },
    close() {
      this.$emit("close");
    },
    selectionChange(selection) {
      this.selection = selection;
    },
    //加载用户数据
    loadDatas() {
      this.$refs.data_table.clearSelection();
      let selectedIds = [];
      if (this.filters.length > 0) {
        this.filters.forEach(item => {
          selectedIds.push(item.id);
        })
      }
      this.axios({
        url: "management/authorization/role/query",
        method: "POST",
        headers: {'json.excludeProperties': 'data.*\.users'},
        data: {
          ops: "notin",
          property: "id",
          value: selectedIds,
          size: this.data.size,
          page: this.data.page - 1,
          sorts: [{property: "id", direction: "asc"}]
        }
      }).then(resp => {
        this.data.data = resp.data.data
        this.data.size = resp.data.size
        this.data.page = resp.data.page + 1
        this.data.total = resp.data.total
        this.loaded = true;
      })
    }
  },
  watch: {
    visible(nv) {
      let me = this;
      me.data.data = [];
      if (nv) {
        this.$nextTick(function () {
          me.loadDatas();
        })
      }
    }
  }
}
</script>

<style scoped>
</style>
